package com.myraceengineer.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.myraceengineer.LabelsManager;
import com.myraceengineer.R;
import com.myraceengineer.model.Segment;

public class SegmentListAdapter extends ArrayAdapter<Segment> {
	private final List<Segment> values;
	private final LayoutInflater inflater;

	public SegmentListAdapter(Context context, List<Segment> values) {
		super(context, R.layout.segment);
		this.values=new ArrayList<Segment>();
		this.values.add(new Segment("track","Session","",LabelsManager.labels[17*8+3],LabelsManager.labels[17*8+5],LabelsManager.labels[17*8+7]));
		this.values.add(new Segment("track", "Lap","",LabelsManager.labels[1*8+3],LabelsManager.labels[1*8+5],LabelsManager.labels[1*8+7]));
		this.values.addAll(values);
		addAll(this.values);
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView;
		if (convertView == null) {
			rowView = inflater.inflate(R.layout.segment, parent, false);
		} else {
			rowView = convertView;
		}
		((TextView) rowView.findViewById(R.id.segnumber)).setText(values.get(position).getNumber());
		int id=R.drawable.n1;
		switch(position+1) {
		case 1: id=R.drawable.ns;break;
		case 2: id=R.drawable.nl;break;
		case 3: id=R.drawable.n1;break;
		case 4: id=R.drawable.n2;break;
		case 5: id=R.drawable.n3;break;
		case 6: id=R.drawable.n4;break;
		case 7: id=R.drawable.n5;break;
		case 8: id=R.drawable.n6;break;
		case 9: id=R.drawable.n7;break;
		case 10: id=R.drawable.n8;break;
		case 11: id=R.drawable.n9;break;
		case 12: id=R.drawable.n10;break;
		case 13: id=R.drawable.n11;break;
		case 14: id=R.drawable.n12;break;
		case 15: id=R.drawable.n13;break;
		case 16: id=R.drawable.n14;break;
		case 17: id=R.drawable.n15;break;
		case 18: id=R.drawable.n16;break;
		case 19: id=R.drawable.n17;break;
		}
		((ImageView) rowView.findViewById(R.id.imgnumber)).setImageDrawable(getContext().getResources().getDrawable(id));
		//((TextView) rowView.findViewById(R.id.time)).setText((values.get(position).getTime());
		((TextView) rowView.findViewById(R.id.avgt)).setText(values.get(position).getTemperature());
		((TextView) rowView.findViewById(R.id.avgp)).setText(values.get(position).getPresure());
		((TextView) rowView.findViewById(R.id.avggrip)).setText(values.get(position).getUsedGrip());
		try {
			int pct=Integer.parseInt(values.get(position).getUsedGrip().replace("%",""));
		
		int color=0;
		if (pct>80) {
			color=Color.GREEN;
		}else if (pct>60) {
			color=Color.parseColor("#FFA500");//Orange
		} else {
			color=Color.RED;
		}
		rowView.setBackgroundColor(color);
	} catch(RuntimeException e) {} 
		
		
	
		
		
		
		return rowView;
	}

}
